#ifndef   CEF_REQUEST_CLIENT_H_
#define CEF_REQUEST_CLIENT_H_


#pragma once

#include <string>
#include <conio.h>

#include "include/base/cef_bind.h"
#include "include/base/cef_callback.h"
#include "include/base/cef_logging.h"
#include "include/cef_urlrequest.h"
#include "include/wrapper/cef_helpers.h"

namespace client {
	class RequestClient : public CefURLRequestClient {
	public:
		// Callback to be executed on request completion.
		typedef base::Callback<void(CefURLRequest::ErrorCode /*error_code*/,
			const std::string& /*download_data*/)>
			Callback;

		//plicit RequestClient(const Callback& callback) : callback_(callback) {
		explicit RequestClient( ) {
			CEF_REQUIRE_UI_THREAD();
			//DCHECK(!callback_.is_null());
		}

		void Detach() {
			CEF_REQUIRE_UI_THREAD();
			if (!callback_.is_null())
				callback_.Reset();
		}

		void OnRequestComplete(CefRefPtr<CefURLRequest> request) OVERRIDE {
			CEF_REQUIRE_UI_THREAD();
			_cprintf("request complete.\n");
			//if (!callback_.is_null()) {
			//	callback_.Run(request->GetRequestError(), download_data_);
			//	callback_.Reset();
			//}
		}

		void OnUploadProgress(CefRefPtr<CefURLRequest> request,
			int64 current,
			int64 total) OVERRIDE {}

		void OnDownloadProgress(CefRefPtr<CefURLRequest> request,
			int64 current,
			int64 total) OVERRIDE {}

		void OnDownloadData(CefRefPtr<CefURLRequest> request,
			const void* data,
			size_t data_length) OVERRIDE {
			CEF_REQUIRE_UI_THREAD();
			_cprintf("request downloaddata  complete.\n");
			download_data_ += std::string(static_cast<const char*>(data), data_length);
			_cprintf("request downloaddata  complete.  [%s]\n", download_data_.c_str());
		}

		bool GetAuthCredentials(bool isProxy,
			const CefString& host,
			int port,
			const CefString& realm,
			const CefString& scheme,
			CefRefPtr<CefAuthCallback> callback) OVERRIDE {
			return false;
		}

	private:
		Callback callback_;
		std::string download_data_;

		IMPLEMENT_REFCOUNTING(RequestClient);
		DISALLOW_COPY_AND_ASSIGN(RequestClient);
	};
}

#endif // !  CEF_REQUEST_CLIENT_H_